﻿Imports System.Configuration
Imports Microsoft.VisualBasic

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Xml

Public Class CoreClass
    Inherits System.Web.UI.Page

    Public ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString").ToString
    Public objData As New DatabaseClass(ConnectionString)
    Public objDataRead As New DatabaseClass(ConnectionString)
    Public objDataRead1 As New DatabaseClass(ConnectionString)
    Public objFunction As New FunctionClass
    Public objConfig As New Configs
    Public objAlert As New AlertClass
    Public sql As String = ""
    Public MenuID As Integer = 0

    ' Khai bao bien de kiem tra quyen
    Public chkView As Int16 = -1
    Public chkUpdate As Int16 = -1
    Public chkDelete As Int16 = -1

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objData.ConnectData()
        objDataRead.ConnectData()
    End Sub

    Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        objData.CloseData()
        objData = Nothing
        objDataRead.CloseData()
        objDataRead = Nothing
        objDataRead1.CloseData()
        objDataRead1 = Nothing
        objFunction = Nothing
    End Sub

    Public Function getErr1(ByVal mess1 As String, _
                            Optional ByVal mess2 As String = vbNullString, _
                            Optional ByVal mess3 As String = vbNullString, _
                            Optional ByVal mess4 As String = vbNullString, _
                            Optional ByVal mess5 As String = vbNullString, _
                            Optional ByVal mess6 As String = vbNullString, _
                            Optional ByVal mess7 As String = vbNullString, _
                            Optional ByVal mess8 As String = vbNullString, _
                            Optional ByVal mess9 As String = vbNullString, _
                            Optional ByVal mess10 As String = vbNullString) As String
        Dim strHTML As String = ""
        strHTML &= "<br /><div class=""err"">"
        strHTML &= "<div class=""err_icon""><!-- --></div>"
        strHTML &= "<a href=""#"" class=""close"" title=""Đóng thông báo"">X</a>"
        strHTML &= "<div class=""desc"">"
        strHTML &= "<span class=""error"">Thông báo Lỗi!</span>"

        If Not mess1 = "" Then
            strHTML &= "<span class=""mess"">- " & mess1 & "</span><br />"
        End If
        If Not mess2 = "" Then
            strHTML &= "<span class=""mess"">- " & mess2 & "</span><br />"
        End If
        If Not mess3 = "" Then
            strHTML &= "<span class=""mess"">- " & mess3 & "</span><br />"
        End If
        If Not mess4 = "" Then
            strHTML &= "<span class=""mess"">- " & mess4 & "</span><br />"
        End If
        If Not mess5 = "" Then
            strHTML &= "<span class=""mess"">- " & mess5 & "</span><br />"
        End If
        If Not mess6 = "" Then
            strHTML &= "<span class=""mess"">- " & mess6 & "</span><br />"
        End If
        If Not mess7 = "" Then
            strHTML &= "<span class=""mess"">- " & mess7 & "</span><br />"
        End If
        If Not mess8 = "" Then
            strHTML &= "<span class=""mess"">- " & mess8 & "</span><br />"
        End If
        If Not mess9 = "" Then
            strHTML &= "<span class=""mess"">- " & mess9 & "</span><br />"
        End If
        If Not mess10 = "" Then
            strHTML &= "<span class=""mess"">- " & mess10 & "</span><br />"
        End If

        strHTML &= "&nbsp;</div>"
        strHTML &= "</div>"
        Return strHTML
    End Function

    Public Function getErr(ByVal al As ArrayList) As String
        Dim strHTML As String = ""
        strHTML &= "<br /><div class=""err"">" & vbCrLf
        strHTML &= "<div class=""err_icon""><!-- --></div>" & vbCrLf
        strHTML &= "<a href=""#"" class=""close"" title=""Đóng thông báo"">X</a>" & vbCrLf
        strHTML &= "<div class=""desc"">" & vbCrLf
        strHTML &= "<span class=""error"">Thông báo lỗi!</span>" & vbCrLf

        For i = 0 To al.Count - 1
            strHTML &= "<span class=""mess"">- " & al.Item(i).ToString & "</span><br />" & vbCrLf
        Next

        strHTML &= "&nbsp;</div>" & vbCrLf
        strHTML &= "</div>" & vbCrLf
        Return strHTML
    End Function

    Public Function getWarning(ByVal mess1 As String) As String
        Dim strHTML As String = ""
        strHTML &= "<br /><div class=""warning"">" & vbCrLf
        strHTML &= "<div class=""warn_icon""><!-- --></div>" & vbCrLf
        strHTML &= "<a href=""#"" class=""close"" title=""Đóng thông báo"">X</a>" & vbCrLf
        strHTML &= "<div class=""desc"">" & vbCrLf
        strHTML &= "<span class=""error"">Chú ý!</span>" & vbCrLf
        strHTML &= "<span class=""mess"">- " & mess1 & "</span><br />" & vbCrLf
        strHTML &= "&nbsp;</div>" & vbCrLf
        strHTML &= "</div>" & vbCrLf
        Return strHTML
    End Function

    Public Sub chkPms(ByVal groupID As Int16, ByVal menuID As Int16)
        Try
            'MsgBox("group: " & Session("GroupId").ToString & "     Menu: " & menuID)
            sql = "spi_chkPms " & menuID & ", " & groupID
            objData.ExeReader(sql)
            Do While objData.dataRead.Read
                chkView = CInt(objData.dataRead("PmsView").ToString())
                chkUpdate = CInt(objData.dataRead("PmsUpdate").ToString())
                chkDelete = CInt(objData.dataRead("PmsDelete").ToString())
            Loop
        Catch ex As Exception
            'MsgBox("CoreClass sub chkPms() : " & ex.Message)
            Response.Redirect("/Inside/xLogin.aspx?redirect=" & Request.Url.PathAndQuery.Replace("?", "|"))
        End Try
    End Sub

    ' Hiển thị trạng thái
    Public Function getStt(ByVal stt As Integer) As String
        If stt = 0 Then
            Return "Lock \ Unavaiable"
        Else
            Return "Avaiable"
        End If
    End Function

    ' Format 1 va 0 sang true va false
    Public Function formatBool(ByVal code As String) As String
        If code = "1" Then
            Return "true"
        Else
            Return "false"
        End If
    End Function

    ' Function get Menu ID to check permission
    Public Function getIDMenu(ByVal code As Integer) As Integer
        Try
            Dim returnValue As Integer = 0
            Dim strHTML As New StringBuilder()
            Dim xml As String = ""
            xml = objFunction.ReadEndFile(Server.MapPath("/") & "static\inSite\ListFile\MenuNavigator.xml")
            Dim rgx As New Regex("\n|\t|\r")
            xml = rgx.Replace(xml, "")
            Dim doc As New XmlDocument()
            doc.LoadXml(xml)
            Dim nodeList As XmlNodeList = doc.GetElementsByTagName("menu")
            For Each n As XmlNode In nodeList
                Dim xml_id As String = n.SelectSingleNode("id").InnerText
                Dim xml_name As String = n.SelectSingleNode("name").InnerText
                Dim xml_code As String = n.SelectSingleNode("code").InnerText
                If Integer.Parse(xml_code) = code Then
                    returnValue = Integer.Parse(xml_id)
                End If
            Next
            Return returnValue
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try

    End Function

End Class
